package com.ayoka.ui.training;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

import com.ayoka.Constant;
import com.ayoka.R;
import com.ayoka.adapter.TrainingGroupSectionAdapter;
import com.ayoka.base.BaseRecyclerViewActivity;
import com.ayoka.bean.TrainingBean;
import com.ayoka.bean.UserInformation;
import com.ayoka.databinding.ActivityBaseRecyclerviewBinding;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupModel.TrainingGroupBean;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupModel.TrainingGroupSectionEntity;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupPresenter;
import com.ayoka.mvp.TrainingGroupMVP.ITrainingGroupView;
import com.ayoka.ui.member.OpenMemberActivity;
import com.ayoka.utils.MemberEvent;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;
import com.jcp.baselibrary.brvah.RecycleViewDivider;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.List;
import java.util.Observable;


/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class TrainingGroupActivity extends BaseRecyclerViewActivity<TrainingGroupSectionEntity, ActivityBaseRecyclerviewBinding, TrainingGroupPresenter> implements ITrainingGroupView {

    public static void start(Activity activity, long courseId, long groupId) {
        Intent intent = new Intent(activity, TrainingGroupActivity.class);
        intent.putExtra("CourseId", courseId);
        intent.putExtra("GroupId", groupId);
        activity.startActivity(intent);
    }

    private long courseId, groupId;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null){
            courseId = intent.getLongExtra("CourseId", 0);
            groupId = intent.getLongExtra("GroupId", 0);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_base_recyclerview;
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh ;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mDataBinding.recyclerView.setBackgroundColor(UIUtils.getColor(R.color.white_f2));
        return mDataBinding.recyclerView;
    }

    @Override
    protected BaseQuickAdapter<TrainingGroupSectionEntity, BaseViewHolder> getAdapter() {
        TrainingGroupSectionAdapter adapter = new TrainingGroupSectionAdapter(Constant.TrainingType.BASIC);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TrainingGroupSectionEntity item = (TrainingGroupSectionEntity) adapter.getItem(position);
                if (item.isHeader) return;
                TrainingBean trainingBean = item.t;
                if (trainingBean != null){
                    if (trainingBean.trainingType == Constant.TrainingType.BASIC){
                        if (trainingBean.CourseCompleted == 0){
                            showToast(getString(R.string.please_complete_course_first));
                        } else {
                            TrainingPrepareActivity.start(TrainingGroupActivity.this, trainingBean.ID, trainingBean.Title);
                        }
                    } else if (trainingBean.trainingType == Constant.TrainingType.ADVANCED){
                        if (trainingBean.IsAvailabe == 0){
                            mPresenter.showOpenMemberDialog(getSupportFragmentManager(), "进阶联系需要开通会员才能继续，请去开通会员");
                        } else {
                            if (trainingBean.CourseCompleted == 0) {
                                showToast(getString(R.string.please_complete_course_first));
                            } else {
                                TrainingPrepareActivity.start(TrainingGroupActivity.this, trainingBean.ID, trainingBean.Title);
                            }
                        }
                    }
                }
            }
        });
        return adapter;
    }

    @Override
    protected boolean getLoadEnable() {
        return false;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return new RecycleViewDivider.Builder(this)
                .setStyle(RecycleViewDivider.Style.BOTH)
                .setColor(Color.TRANSPARENT)
                .setSize(TypedValue.COMPLEX_UNIT_PX, UIUtils.getDimensionPixelSize(R.dimen.dp_15))
                .build();
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        mPresenter.getTrainingGroupData(isShowWaitDialog, courseId, groupId);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.training_group);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        if (UserInformation.getInstance().user_info.IsVip == 0){
            MemberEvent.getInstance().addObserver(this);
        }
        getData(true);
    }

    @Override
    protected TrainingGroupPresenter getPresenter() {
        return new TrainingGroupPresenter();
    }

    @Override
    public void configTrainingGroupData(TrainingGroupBean trainingGroupBean) {
        if (trainingGroupBean == null){
            setLoadMoreLayout(null);
        } else {
            List<TrainingGroupSectionEntity> result = mPresenter.formatList(trainingGroupBean);
            setLoadMoreLayout(result);
        }
    }

    @Override
    public void onOpenMemberClick() {
        OpenMemberActivity.start(this, Constant.OpenMember.OPEN_MEMBER);
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        if (observable instanceof MemberEvent){
            if (mPresenter != null){
                onRefresh();
            }
        }
    }

    @Override
    protected void onDestroy() {
        MemberEvent.getInstance().deleteObserver(this);
        super.onDestroy();
    }
}
